package com.sanjiahantang.ythshopping.model;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanjiahantang.ythshopping.utils.SPUtils;

import java.util.Comparator;
import java.util.List;

/**
 * Created by zy on 2017/9/18.
 */

public class AreaBean extends BaseModel {
    public static final String HISTORY_CITY = "historyCity";
    private Data data;

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public static class Data{
        private List<City> hot_city;
        private List<PinYinCity> pinyin_city;
        private List<String> pinyin;

        public List<City> getHot_city() {
            return hot_city;
        }

        public void setHot_city(List<City> hot_city) {
            this.hot_city = hot_city;
        }

        public List<PinYinCity> getPinyin_city() {
            return pinyin_city;
        }

        public void setPinyin_city(List<PinYinCity> pinyin_city) {
            this.pinyin_city = pinyin_city;
        }

        public List<String> getPinyin() {
            return pinyin;
        }

        public void setPinyin(List<String> pinyin) {
            this.pinyin = pinyin;
        }
    }

    public static class City{
        private String region_id;
        private String parent_name;
        private String region_name;
        private String pinyin;

        public City(){

        }

        public City(String region_name,String pinyin){
            this.region_name = region_name;
            this.pinyin = pinyin;
        }

        public String getParent_name() {
            return parent_name;
        }

        public void setParent_name(String parent_name) {
            this.parent_name = parent_name;
        }

        public String getRegion_id() {
            return region_id;
        }

        public void setRegion_id(String region_id) {
            this.region_id = region_id;
        }

        public String getRegion_name() {
            return region_name;
        }

        public void setRegion_name(String region_name) {
            this.region_name = region_name;
        }

        public String getPinyin() {
            return pinyin;
        }

        public void setPinyin(String pinyin) {
            this.pinyin = pinyin;
        }
    }


    public static class PinYinCity{
        private String pinyin;
        private List<City> city;

        public String getPinyin() {
            return pinyin;
        }

        public void setPinyin(String pinyin) {
            this.pinyin = pinyin;
        }

        public List<City> getCity() {
            return city;
        }

        public void setCity(List<City> city) {
            this.city = city;
        }
    }

    public static class CompareSort implements Comparator<City> {

        @Override
        public int compare(City city1, City city2) {
            if(city1.getPinyin().equals("@") || city2.getPinyin().equals("@")){
                //通讯录前面的ｉｔｅｍ(公众号，标签......)
                return city1.getPinyin().equals("@") ? -1:1;
            }
            //user1属于#标签，放到后面
            else if(!city1.getPinyin().matches("[A-z]+")){
                return 1;
                //user2属于#标签，放到后面
            }else if(!city2.getPinyin().matches("[A-z]+")){
                return -1;
            }else {
                return city1.getPinyin().compareTo(city2.getPinyin());
            }
        }
    }

    public static void addHistory(Context context,City city){
        String history = (String) SPUtils.get(context,HISTORY_CITY,"[]");

        Gson gson = new Gson();
        List<City> cities = gson.fromJson(history,  new TypeToken<List<City>>() {  }.getType());
        int i = 0,max = cities.size();
        for (;i < max;i++){
            if(cities.get(i).getRegion_name().equals(city.getRegion_name())){
                break;
            }

            if(cities.get(i).getRegion_name().equals(city.getRegion_name() + "市")){
                if(TextUtils.isEmpty(cities.get(i).getRegion_id())){
                    cities.get(i).setRegion_id(city.getRegion_id());
                    cities.get(i).setPinyin(city.getPinyin());
                }
                break;
            }
        }

        if(i >= max){
            cities.add(city);
            SPUtils.put(context,HISTORY_CITY,gson.toJson(cities));
        }

    }

    public static List<City> getHistory(Context context){
        return new Gson().fromJson((String) SPUtils.get(context,HISTORY_CITY,"[]"),new TypeToken<List<City>>() {  }.getType());
    }
}
